var UCDS = UCDS || {};
(function() {
    function showCommon(o) {
        var self = this;
        console.log(o)
        var option = $.extend({
            url: null,
            dom: document.body,
            eventMode: "click",
            dataType: "url",
            callBank: null
        }, o)

        self.html = "";
        if (!option.url) return;
        self.setPos = function(o){
			var maxWidth = $(window).width();
			var popw = self.$win.outerWidth();
			var popX = self.$dom.offset().left - popw/2;
			var popY = self.$dom.offset().top + self.$dom.innerHeight();
			if(popX+popw >= maxWidth){
				popX = maxWidth - popw;
			}
			if(popX + popw - 70 < self.$dom.offset().left){
				popX = 	self.$dom.offset().left - (popw-70);
			}
			
			if(!o)
				self.$win.css({left:popX,top:popY});
			else{
				self.$win.css({left:o.x,top:o.y});
			}
			self.$win.css({'position':'absolute','z-index':99999});
			//计算尖尖的位置
			var $jj = self.$pop.find(".pop_panel .pop_container .pop_header p");
			var left = (popX+popw-70) - self.$dom.offset().left;
			$jj.css("background-position",(-10-left)+"px 0");
			return self;	
		}
		
		var $dom = self.$dom = $(option.dom);
		var $pop = self.$pop = $("<div style='height:0;' />").appendTo($(document.body));
		var $win;
        function init(){
			$win = self.$win = $pop.children().hide();
				$dom.bind(option.eventMode,function(){
					self.setPos().show();
					return false;
				});
				if(option.callBank){
					option.callBank(self);	
				}			
				$dom.bind("mouseleave",function(){
					$pop.bind("mouseleave",function(){
						$win.hide();
					}).mouseover(self.show);
					$win.hide();
				});
				$(window).resize(function(){
					try{self.setPos();}catch(e){}
				});	
		}
        if(option.dataType == "html"){//带默认容器的html
			var popWin = '<!-- abs_signup --><div class="abs_signup"><!-- pop_panel --><div class="pop_panel"><!-- pop_container --><div class="pop_container"><!-- pop_header --><div class="pop_header"><div><p></p></div></div><!-- end pop_header --><!-- pop_outer --><div class="pop_outer"><!-- pop_inner --><div class="pop_inner"><div class="pop_left"></div><div class="pop_right"></div><!-- pop_top --><div class="pop_top"><div class="pop_btm"></div></div><!-- end pop_top --><!-- pop_content --><div class="pop_content"></div><!-- end pop_content --></div><!-- end pop_inner --></div><!-- end pop_outer --><!-- pop_footer --><div class="pop_footer"><div><p></p></div></div><!-- end pop_footer --></div><!-- end pop_container --></div><!-- end pop_panel --></div><!-- end abs_signup -->';
			$pop.html(popWin);
			$pop.find(".pop_content").html(option.url);
			init();
		}else if(option.dataType == "url"){//路径
			$pop.load(option.url,function(){
				init();
			});	
		}else if(option.dataType == "noContainerHtml"){//不带默认容器的html
			$pop.html("<div>"+option.url+"</div>");
			init();	
		}
		self.hide = function(){
			$win.hide();
			//self.$pop.hide();
			return self;
		}	
		self.show = function(){
			//self.$pop.show();
			$win.show();
			return self;
		}
    }
    UCDS.showCommon = showCommon;
    console.log(UCDS.showCommon)
})(UCDS);

